﻿using M_Manufacture.API.Application.Command.ProductionPlanCommand;
using M_Manufacture.Domain;
using M_Manufacture.Repository.Interface;
using MediatR;

namespace M_Manufacture.API.Application.CommandHandlers.ProductionPlanCommandHandler
{
    public class CreateProductionPlanCommandHandler : IRequestHandler<CreateProductionPlanCommand, int>
    {
        /// <summary>
        /// 生产计划仓储接口
        /// </summary>
        protected readonly IProductionPlanRepository productionPlanRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionPlanRepository">生产计划仓储接口</param>
        public CreateProductionPlanCommandHandler(IProductionPlanRepository productionPlanRepository)
        {
            this.productionPlanRepository = productionPlanRepository;
        }

        /// <summary>
        /// 处理创建生产计划命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(CreateProductionPlanCommand request, CancellationToken cancellationToken)
        {
            return await productionPlanRepository.AddAsync(new ProductionPlan()
            {
                ProductionPlanNumber= $"JH{DateTime.Now.ToString("yyyyMMddHHmmssf")}",
                ProductionStatus= "未排程",
                ProductNumber= $"CP{DateTime.Now.ToString("yyyyMMddHHmmssf")}",
                Priority="不紧急",
                FinishedProductSpecification="规格一",
                PlannedProduction=request.PlannedProduction,
                ReportingNum=request.ReportingNum,
                Id=request.Id,
                ProcessRouteName="工序一",
                WorkshopName="车间一",
                StartingDate=null,
                EndingDate=null,
                CreatedBy = "张三",
                CreatedDate = DateTime.Now
            });
        }
    }
}
